﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.Setting
{
    public partial class ucBank : UserControl
    {
        #region UserControl

        public ucBank()
        {
            InitializeComponent();
            loadData();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = Main.AeroColor;
        }

        #endregion

        #region Event

        private void lvBank_DoubleClick(object sender, EventArgs e)
        {
            this.btnAdd.Enabled = true;
            this.btnModify.Enabled = false;
            this.tbName.Text = this.lvBank.SelectedItems[0].SubItems[1].Text;
            this.cbbBank.SelectedValue = this.lvBank.SelectedItems[0].SubItems[5].Text;
            this.tbAccount.Text = this.lvBank.SelectedItems[0].SubItems[3].Text;
            this.cbDefault.Checked = bool.Parse(this.lvBank.SelectedItems[0].SubItems[6].Text);
            this.pLeft.Tag = this.lvBank.SelectedItems[0].Tag;
            this.pLeft.Visible = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.btnAdd.Enabled = false;
            this.btnModify.Enabled = true;
            this.pLeft.Visible = true;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            lvBank_DoubleClick(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            toolStripMenuItem_Delete_Click(sender, e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbName.Text))
            {
                MessageBox.Show("显示名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(this.tbAccount.Text))
            {
                MessageBox.Show("银行账号不能为空");
                return;
            }

            int rval = 0;
            if (this.btnModify.Enabled) rval = MoneyClass.UserBank.Add(Main.CurrentGuid,this.tbName.Text, this.cbbBank.SelectedValue.ToString(), this.tbAccount.Text, this.cbDefault.Checked ? "true" : "false");
            else rval = MoneyClass.UserBank.Update(this.pLeft.Tag.ToString(), Main.CurrentGuid, this.tbName.Text, this.cbbBank.SelectedValue.ToString(), this.tbAccount.Text, this.cbDefault.Checked ? "true" : "false");

            switch (rval)
            {
                case -2:
                    MessageBox.Show("名称或银行账号已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    break;
                case 1:
                    MessageBox.Show("添加成功");
                    InitListView();
                    break;
                default:
                    MessageBox.Show("内部错误，代码：" + rval);
                    break;
            }
        }

        private void toolStripMenuItem_Delete_Click(object sender, EventArgs e)
        {
            if (this.lvBank.SelectedItems.Count == 0) return;
            if (MoneyClass.UserBank.Delete(this.lvBank.SelectedItems[0].Tag.ToString()) != 1)
            {
                MessageBox.Show("操作失败");
                return;
            }

            InitListView();
        }

        private void pbHide_Click(object sender, EventArgs e)
        {
            this.pLeft.Visible = false;
        }

        #endregion

        #region Method

        private void loadData()
        {
            InitComboBox();
            InitListView();
        }

        private void InitComboBox()
        {
            DataTable dt = MoneyClass.Bank.GetAll();
            this.cbbBank.DisplayMember = "BankName";
            this.cbbBank.ValueMember = "guid";
            this.cbbBank.DataSource = dt;
        }

        private void InitListView()
        {
            DataTable dt = MoneyClass.UserBank.Get(Main.CurrentGuid);
            this.lvBank.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = (this.lvBank.Items.Count + 1).ToString();
                item.SubItems.Add(dr["Ident"].ToString());
                item.SubItems.Add(dr["BankName"].ToString());
                item.SubItems.Add(dr["AccountNo"].ToString());
                item.SubItems.Add(dr["status"].ToString());
                item.SubItems.Add(dr["bid"].ToString());
                item.SubItems.Add(dr["isdef"].ToString());
                item.Tag = dr["guid"].ToString();
                this.lvBank.Items.Add(item);
            }

            this.tsslMsg.Text = string.Format("共{0}条记录", this.lvBank.Items.Count);
        }

        #endregion
    }
}
